import lib_city from "../../../api/lib_city";
import _checked_city from "../../../cache/_checkd_city";
import _local_city from "../../../cache/_local";
import _local from "../../../cache/_local";

Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    letter: [], //字母
    city_list: [], // 城市
    hot_list: [], //热门城市
    current_letter: 'A',
    toView: 'A', // 左侧滚动
    checked_city: '', //选择的城市
    is_loacl: false, //是否开启了定位
    loacl_city: '' //定位的城市

  },
  onShow() {

    lib_city.list().then((res)=>{
      this.setData({
        letter: res.data.letter,
        hot_list: res.data.hot_list,
        city_list: res.data.city_list
      })
    })
    tt.getSetting({
      success: (res) => {
        if (res.errMsg == 'getSetting:ok') {
          if (res.authSetting['scope.userLocation'] == true) {
            this.setData({
              is_loacl: true,
              loacl_city: _local_city.get(),
              checked_city: _checked_city.get()
            })
          } else {
            this.setData({
              is_loacl: false,
              loacl_city: _local_city.get(),
            })
          }

        }
      },
      fail: (res) => {
        console.log(res, '==');
      },
    });
  },

  /**methods */
  //定位
  get_location() {
    tt.getLocation({
      success: (res) => {
        // 转换成对应的城市
        this.setData({
          is_loacl: true,
          loacl_city: res.city
        });

        //存local_city
        _local_city.set(this.data.loacl_city)
      },
      fail: (res) => {
        this.setData({ is_loacl: false });
        tt.showToast({
          title: '请开启地理位置权限',
          icon: 'none'
        });
      }
    });
  },
  //选择城市
  checked_city(e) {
    let checked_city = e.currentTarget.dataset.item;
    tt.navigateTo({
      url: `/pages/home/home?checked_city=${checked_city}`
    });

    //checked_city
    _checked_city(checked_city)
  },

  clk_letter(e) {
    this.setData({
      current_letter: e.currentTarget.dataset.item,
      toView: e.currentTarget.dataset.item,
    })
  }
})
